package com.oracle.lingshangejia.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;


import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.LocationInfo;

import java.util.ArrayList;


public class PositionListAdapter extends RecyclerView.Adapter<PositionListAdapter.ViewHolder> {

    private Context context;
    private ArrayList<LocationInfo> mList;
    private OnItemClickListener listener;
    public interface OnItemClickListener {
        void onItemClick(View view, int whyId);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    public PositionListAdapter(Context context, ArrayList<LocationInfo> mList){
        this.context = context;
        this.mList=mList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view =View.inflate(context,R.layout.item_position,null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder,final int i) {
        viewHolder.tv_name.setText(mList.get(i).getName());
        viewHolder.tv_address.setText(mList.get(i).getAddress());
        if(i==0){
            viewHolder.rb_choice.setVisibility(View.VISIBLE);
            viewHolder.rb_choice.setChecked(true);
        }else{
            viewHolder.rb_choice.setChecked(false);
            viewHolder.rb_choice.setVisibility(View.GONE);
        }
       viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (listener != null) {
                   listener.onItemClick(v,i);
               }
           }
       });
    }

    @Override
    public int getItemCount() {
        return mList!=null?mList.size():0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
          TextView tv_name;
          TextView tv_address;
          RadioButton rb_choice;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_address=itemView.findViewById(R.id.tv_address);
            rb_choice=itemView.findViewById(R.id.rb_choice);
        }
    }

}
